<?php

namespace Home\Controller;

// use Think\Controller;
// use Vendor\httpful-2.phar;
//import("Home.Library.httpful-2.phar");


class Sslogin {
	
	
	 function getCookie() {
		$url = 'http://www.ishansong.com/user/login';
		$resp = \Httpful\Request::get ( $url )->send ();
	
		$resp_cookies = $resp->headers ['set-cookie'];
		$cookie = explode ( ';', $resp_cookies ) [0];
	
		$req_cookie = $cookie;
	
		$url = "http://www.ishansong.com/user/doLogin";
		$body = array (
				'username' => '18500238618',
				'password' => 'jidongjidong114'
		);
	
		$req = \Httpful\Request::post ( $url )->body ( $body )->addHeader ( 'Cookie', $req_cookie )->sendsType ( \Httpful\Mime::FORM )->followRedirects ( false );
	
		$resp = $req->send ();
		$headers = $resp->headers;
	
		$cookie = $resp->headers ['set-cookie'];
	
		$cookie = explode ( ';', $cookie ) [0];
		$req_cookie = $req_cookie . '; ' . $cookie;
	
		// $req = \Httpful\Request::get('http://www.ishansong.com/web/admin/order/list')->addHeader('Cookie', $req_cookie)->followRedirects(false);
		// $resp = $req->send();
		// echo ($resp->body);
	
		// $req = \Httpful\Request::get('http://www.ishansong.com/web/admin/order/list?page=1&sortType=&&qstatus=-1')->addHeader('Cookie', $req_cookie)->followRedirects(false);
		// $resp = $req->send();
	
		// echo ($resp->body);
	
		return $req_cookie;
	}
	
	 function ssvisitpage($url, $req_cookie) {
		// $req = \Httpful\Request::get ( 'http://www.ishansong.com/web/admin/order/list?page='.$page.'&sortType=&&qstatus=-1' )->addHeader ( 'Cookie', $req_cookie )->followRedirects ( false );
		$req = \Httpful\Request::get ( $url )->addHeader ( 'Cookie', $req_cookie )->followRedirects ( false );
	
		$resp = $req->send ();
	
		// echo ($resp->body);
	
		return $resp->body;
	}
	
	
	public function load() {
		require_once C('CUSTOM_LIBRARY') . '/httpful-2.phar';
		
		
		
		// echo $mobile;
		date_default_timezone_set ( 'Asia/Shanghai' );
		$date = localtime ();
		
		$date2 = date ( 'Y-m-d' );
		$date1 = date ( 'Y-m-d', strtotime ( '-14 day' ) );
		
		
		$path =  C('SS_CACHEPATH');
		$pagefile = $path . $date2 . '_' . $date1 . '_';
		
		echo $pagefile . "</br>res=";
		
		//产生抓取页面的缓存
		$this->getSsCache($pagefile);
		
		//分析页面，获取其中的条目
	
	
	}
	
	 function getSsCache($pagefile) {
		/*

		$req_cookie = $this->getCookie ();
		$p = 2;
		for($x = 1; $x < $p; $x = $x + 1) {
			// http://www.ishansong.com/web/admin/order/list?sdate=2015-12-13&edate=2015-12-19&page=1&qstatus=-1&keyword=
				
			$filepath = $pagefile . $x;
			if (file_exists ( $filepath )) {
				continue;
			}
			else
			{
		
				$url = "http://www.ishansong.com/web/admin/order/list?sdate=" . $date1 . "&edate=" . $date2 . "&page=" . $x . "&qstatus=-1&keyword=";
		
				$content = $this->ssvisitpage ( $url, $req_cookie );
				if ($x == 1) {
					$p = $this->getPageNum ( $content );
					echo "</br>p=" . $p;
					if($p==0)
					{
						break;
					}
				}
		
				$res = file_put_contents ( $filepath, $content );
		
				echo $res . "</br>res=" . $res . " file=" . $filepath;
			}
		}
		*/
		
	}
	
	
	
	public function loadtest() {
		$url = "http://localhost/tp323/Public/pc/11.page";
		
		// 查找出来总页数
		$req_cookie = null;
		
		$req = \Httpful\Request::get ( $url )->addHeader ( 'Cookie', $req_cookie )->followRedirects ( false );
		
		$resp = $req->send ();
		$resp = $resp->body;
		// echo ($resp->body);
		// <div class="dataTables_info pull-right" id="sample_1_info">总共 139 页，1388 条记录，每页 10 条记录</div>
		
		$strtmp = "总共 ";
		$strtmplen = strlen ( $strtmp );
		
		$pos = stripos ( $resp, $strtmp );
		$pos2 = stripos ( $resp, "页，" );
		
		$str = substr ( $resp, $pos + $strtmplen, $pos2 - $pos - $strtmplen );
		$pagenum = intval ( str );
		echo $pos . " ###  " . $pos2 . "</br>";
		echo $str . "</br>";
	}
	function getPageNum($content) {
		$strtmp = "总共 ";
		$strtmplen = strlen ( $strtmp );
		
		$pos = stripos ( $content, $strtmp );
		$pos2 = stripos ( $content, "页，" );
		
	//	echo  "</br>".$pos . " ####  " . $pos2 . "</br>".$str;
		
		$str = substr ( $content, $pos + $strtmplen, $pos2 - $pos - $strtmplen-1 );
		trim($str);
		$pagenum =intval($str);
		
	//	echo  "</br>".$pos . " ####  " . $pos2 ."page=".$pagenum. "</br>##".$str."##";
		
		return $pagenum;
	}
	
	// function sswritecache($path, $content) {
	// // 写文件,10次， 日期-page.txt
	// $path = '/Library/WebServer/Documents/webyouzan/ssgit/sspagefile/';
	// // $content=$resp->body;
	// $p = $date1 . '-' . $date2 . '-' . $page;
	
	// $pagefile = $path . $p . '.txt';
	
	// echo $pagefile . "</br>";
	// $res = file_put_contents ( $pagefile, $content );
	// echo $res;
	
	// return $res;
	// }
	/*
	 * function sswritecache($page, $content, $date1, $date2) {
	 * // 写文件,10次， 日期-page.txt
	 * $path = '/Library/WebServer/Documents/webyouzan/ssgit/sspagefile/';
	 * // $content=$resp->body;
	 * $p = $date1 . '-' . $date2 . '-' . $page;
	 *
	 * $pagefile = $path . $p . '.txt';
	 *
	 * echo $pagefile . "</br>";
	 * $res = file_put_contents ( $pagefile, $content );
	 * echo $res;
	 *
	 * return $res;
	 * }
	 *
	 */
}